package top.hkyzf.study.leetcode.tree;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-22 14:36
 */
public class 翻转二叉树_226 {
    public TreeNode invertTree(TreeNode root) {
        invert(root);
        return root;
    }

    private void invert(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invert(root.left);
        invert(root.right);
    }

    @Test
    public void testInvertTree () {
        TreeNode node6 = new TreeNode(9);
        TreeNode node5 = new TreeNode(6);
        TreeNode node4 = new TreeNode(3);
        TreeNode node3 = new TreeNode(1);
        TreeNode node2 = new TreeNode(7, node5, node6);
        TreeNode node1 = new TreeNode(2, node3, node4);
        TreeNode root = new TreeNode(4, node1, node2);
        TreeNode node = invertTree(root);
        node.print();
    }
}
